package fr.esiag.isidis.pdsTeam.views;

import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.net.UnknownHostException;
import java.util.Iterator;
import java.util.Map;
import javax.swing.*;


public class ReseauTerrain extends JPanel implements ActionListener{
	
	Image backGround;
	Timer timer;
	Map<Integer,TrainView> trains;
	Map<Integer,LigneView> lignesView;
	
	public ReseauTerrain(Map<Integer,TrainView> trains,Map<Integer,LigneView> lignesView) {
		this.lignesView=lignesView;
		this.trains=trains;
		backGround=ResourceImage.getInstance().getBackGround();
		
		timer=new Timer(20,this);
		timer.start();
		
	}
	



	public void actionPerformed(ActionEvent e) {
		Iterator it = trains.entrySet().iterator();
	    while (it.hasNext()) {
	        Map.Entry pairs = (Map.Entry)it.next();
	        TrainView train=((TrainView)pairs.getValue());
	        try {
				train.move();
			} catch (UnknownHostException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
	    }
		repaint();
	}
	
	@Override
	public void paint(Graphics g) {
		super.paint(g);
		Graphics2D graphics2d =(Graphics2D)g;
		graphics2d.drawImage(backGround, 0, 0,null);
		Iterator it = lignesView.entrySet().iterator();
	    while (it.hasNext()) {
	        Map.Entry pairs = (Map.Entry)it.next();
	        LigneView ligneView=(LigneView)pairs.getValue();
	        graphics2d.drawImage(ligneView.getImage(),0,0,null);
	    }
		
		it = trains.entrySet().iterator();
	    while (it.hasNext()) {
	        Map.Entry pairs = (Map.Entry)it.next();
	        TrainView train=(TrainView)pairs.getValue();
	        graphics2d.drawImage(train.getImage(),train.getX(),train.getY(),null);
	    }
	}
}
